package controllayer;
import modellayer.*;
import java.util.ArrayList;

/**
 * A controller class that handles interaction with sales and ordres.
 * @author (Jonas, Dani og Peter) 
 * @version (09-12-2014)
 */
public class OrdreController
{
    private SalgsContainer salgsContainer;
    private VareController vareController;
    private PersonController personController;
    private static OrdreController instance;
    private Salg openSalg; //An attribute that is referencing the Salg object until the sale is completed.

    /**
     * Constructor for objects of class OrdreController. Instantiates the PersonController, VareController and OrdreContainer.
     */
    private OrdreController()
    {
        salgsContainer = salgsContainer.getInstance();
        personController = personController.getInstance();
        vareController = vareController.getInstance();
    }

    /**
     * Singleton for OrdreController.
     */
    public static OrdreController getInstance()
    {
        if(instance == null)
        {
            instance = new OrdreController();
        }

        return instance;
    }

    /**
     * Instatiates a new Salg object and adds that to the SalgsContainer if a unique ID parameter is passed.
     * 
     * @param  ID  A string that is assigned to the id attribute of Salg
     * @param  dato  A string that is assigned to the dato attribute of Salg
     * @return     A boolean statement signifying whether a Salg is succesfully created
     */
    public boolean createSalg(String ID, String dato)
    {
        if(findSalg(ID) == null)
        {
            openSalg = new Salg(ID, dato);
            salgsContainer.addSalg(openSalg);
            return true;
        }
        else
        {
            return false;
        }

    }

    /**
     * Instatiates a new Salg object and set the levering attribute indicating that this is an ordre. 
     * The object is added to the SalgsContainer if a unique ID parameter is passed.
     * 
     * @param  ID  A string that is assigned to the id attribute of Salg
     * @param  dato  A string that is assigned to the dato attribute of Salg
     * @param  levering  A boolean indicating wheter the Salg should be delivered or not
     * @return     A boolean statement signifying whether a Salg is succesfully created
     */
    public boolean createOrdre(String ID, String dato, boolean levering)
    {
        if(findSalg(ID) == null)
        {
            openSalg = new Salg(ID, dato, levering);
            salgsContainer.addSalg(openSalg);
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Creates and adds a delsalg to the salg object.
     * 
     * @param  stregkode   String to identify a Vare
     * @param  antal   int to indicate the number of Vare
     * @return     a boolean value indicating whether the salg attribute is referencing an object before adding the Kunde object. If not; returns false.
     */
    public boolean addDelSalg(String stregkode, int antal)
    {
        if(openSalg != null)
        {
            Vare addVare = vareController.findVare(stregkode);
            if(addVare != null)
            {
                DelSalg delSalg = new DelSalg(addVare, antal);
                openSalg.addDelSalg(delSalg);
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    /**
     * Prompts the PersonController to find a Kunde with matching parameter value.
     * 
     * @param  telefon   String to identify a Kunde
     * @return     a boolean value indicating whether the salg attribute is referencing an object before adding the Kunde object. If not; returns false.
     */
    public boolean addKunde(String telefon)
    {
        Kunde kunde = personController.findKunde(telefon);
        if(openSalg != null)
        {
            if(kunde != null)
            {    
                openSalg.setKunde(kunde);
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }

    }

    /**
     * Sets the state attribute of salg to the parameter value
     * 
     * @param  state  sets the state attribute on salg.
     * @return     boolean value indicating if the salg is referencing an object before setting its state.
     */
    public boolean setSalgState(boolean state)
    {
        if(openSalg != null)
        {
            openSalg.setState(state);
            return true;
        }
        else
        {
            return false;
        }  
    }

    /**
     * Calls the findSalg on the SalgsContainer and returns a Salg object.
     * 
     * @param  id   String to identify a Salg
     * @return     a Salg object or null if no Salg is found.
     */
    public Salg findSalg(String id)
    {
        Salg returnSalg = salgsContainer.findSalg(id);
        return returnSalg;
    }

    /**
     * Calls the findKunde method on the KundeContainer class, that returns a Kunde object if it exists.
     * 
     * @param  telefon   String to identify a Kunde
     * @return     a Kunde object or null if no Kunde is found.
     */
    public Salg getOpenSalg()
    {
        return this.openSalg;
    }

    /**
     * Prompts the Salg object to calculate the "total"-attribute and returns it.
     * 
     * @return     the "total" attribute on salg.
     */
    public double getTotal()
    {
        if(this.openSalg != null)
        {
            return this.openSalg.getTotal();
        }
        else
        {
            return 0.0;
        }
    }
    
    /**
     * Completes the sale, by calculating the total amount to pay and setting the current openSale to null, to avoid bugs.
     * 
     */
    public boolean completeSalg()
    {
        boolean succes = false;
        if(this.openSalg != null)
        {
            this.openSalg.getTotal();
            this.openSalg = null;
            succes = true;
        }
        return succes;
    }
}
